<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | User控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\UrgeMessageService;
use think\Request;
use think\Exception;

class UrgeMessageController extends VerifyController
{
    /**
     * @var \common\service\UserService
     */
    protected $urgeMessageService;

    public function __construct()
    {
        parent::__construct();
        $this->urgeMessageService = new UrgeMessageService();
    }

    /**
     * 催一催创建
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $res = $this->urgeMessageService->create($data, $request->getUserId());
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 催一催创建
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function getList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->urgeMessageService->getList($param, $request->getUserId());
        return json(success_response("OK", $res));
    }

    /**
     * 更新催一催已读
     * @param Request $request
     * @return \think\Response
     */
    public function updateUrgeMessageRead(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $urgeMessageId = $data["urge_message_id"];
            $res = $this->urgeMessageService->updateUrgeMessageRead($urgeMessageId, $request->getUserId());
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新催一催
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function updateUrgeMessageMember(Request $request)
    {
        $data = $request->param()['data'];
        try {
            $data['user_id'] = $data['user_id'] ?? $request->getUserId();
            $res = $this->urgeMessageService->updateUrgeMessageMember($data);
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
